﻿/*
 * @author: wizardc
 */

using UnityEngine;

namespace DouEditor.Extend
{
    /// <summary>
    /// Editor非运行模式下快捷查看ParticleSystem的工具组件，支持速率调整
    /// </summary>
    [RequireComponent(typeof(ParticleSystem))]
    [DisallowMultipleComponent]
    [ExecuteInEditMode]
    public class ParticleQuickViewer : MonoBehaviour
    {
        public ParticleSystem particleSystem;

        [SerializeField]
        private int _frameRate = 30;
        private float _frameTime = 1f / 30;

        private ParticleEditorPlayInfo _particleEditorPlayInfo;

        private ParticleEditorPlayInfo ParticleEditorPlayInfo
        {
            get
            {
                if (_particleEditorPlayInfo == null)
                {
                    CollectParticleSystem();
                    _particleEditorPlayInfo = ParticleEditorPlayMgr.instance.AddData(particleSystem);
                }
                return _particleEditorPlayInfo;
            }
        }

        public bool withChildren
        {
            set
            {
                if (ParticleEditorPlayInfo != null)
                {
                    ParticleEditorPlayInfo.withChildren = value;
                }
            }
            get
            {
                if (ParticleEditorPlayInfo != null)
                {
                    return ParticleEditorPlayInfo.withChildren;
                }
                return true;
            }
        }

        public bool playing
        {
            get
            {
                if (ParticleEditorPlayInfo != null)
                {
                    return ParticleEditorPlayInfo.playing;
                }
                return false;
            }
        }

        public float currentTime
        {
            set
            {
                if (ParticleEditorPlayInfo != null)
                {
                    ParticleEditorPlayInfo.currentTime = value;
                }
            }
            get
            {
                if (ParticleEditorPlayInfo != null)
                {
                    return ParticleEditorPlayInfo.currentTime;
                }
                return 0;
            }
        }

        public float totalTime
        {
            get
            {
                if (ParticleEditorPlayInfo != null)
                {
                    return ParticleEditorPlayInfo.totalTime;
                }
                return 0;
            }
        }

        public int frameRate
        {
            set
            {
                _frameRate = value;
                _frameTime = 1f / _frameRate;
            }
            get => _frameRate;
        }

        public int currentFrame
        {
            set
            {
                if (totalTime > 0)
                {
                    currentTime = ((float) value / (float) totalFrame) * totalTime;
                }
            }
            get
            {
                if (totalTime == 0)
                {
                    return 0;
                }
                return Mathf.FloorToInt((float) currentTime / (float) totalTime * totalFrame);
            }
        }

        public int totalFrame
        {
            get
            {
                if (totalTime == 0)
                {
                    return 0;
                }
                return Mathf.CeilToInt((float) totalTime / _frameTime);
            }
        }

        public bool isLoop
        {
            set
            {
                if (ParticleEditorPlayInfo != null)
                {
                    ParticleEditorPlayInfo.isLoop = value;
                }
            }
            get
            {
                if (ParticleEditorPlayInfo != null)
                {
                    return ParticleEditorPlayInfo.isLoop;
                }
                return false;
            }
        }

        public float ratio
        {
            set
            {
                if (ParticleEditorPlayInfo != null)
                {
                    ParticleEditorPlayInfo.ratio = value;
                }
            }
            get
            {
                if (ParticleEditorPlayInfo != null)
                {
                    return ParticleEditorPlayInfo.ratio;
                }
                return 1;
            }
        }

        private void CollectParticleSystem()
        {
            particleSystem = GetComponent<ParticleSystem>();
        }

        public void Play()
        {
            ParticleEditorPlayInfo.Play();
        }

        public void Pause()
        {
            ParticleEditorPlayInfo.Pause();
        }

        public void Resume()
        {
            ParticleEditorPlayInfo.Resume();
        }

        public void Stop()
        {
            ParticleEditorPlayInfo.Stop();
        }

        private void OnDestroy()
        {
            ParticleEditorPlayMgr.instance.RemoveData(particleSystem);
        }
    }
}
